package com.md.youyouhui.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.AdapterShoppingInfoBinding;
import com.md.youyouhui.net.response.GetSellResponse;
import com.md.youyouhui.ui.shopping.ShoppingPayActivity;
import com.md.youyouhui.utils.GlideApp;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/5/21.
 */
public class ShoppingInfoAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterShoppingInfoBinding>> {
    private Context context;
    private List<GetSellResponse.Row> sellColumnLinks;

    public ShoppingInfoAdapter(Context context) {
        this.context = context;
    }

    void upDate(List<GetSellResponse.Row> sellColumnLinks) {
        this.sellColumnLinks = sellColumnLinks;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public DataBoundViewHolder<AdapterShoppingInfoBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        AdapterShoppingInfoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.adapter_shopping_info, viewGroup, false);
        return new DataBoundViewHolder<>(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterShoppingInfoBinding> holder, int i) {
        holder.binding.infoNum.setText(String.format("已售%s件", sellColumnLinks.get(i).sales));
        holder.binding.infoPrice.setText(String.format("¥%s", sellColumnLinks.get(i).price));
        holder.binding.infoTitle.setText(sellColumnLinks.get(i).title);
        GlideApp.with(context).load(sellColumnLinks.get(i).cover).into(holder.binding.infoIv);
        holder.itemView.setOnClickListener(v -> context.startActivity(new Intent(context, ShoppingPayActivity.class)
                .putExtra("id",sellColumnLinks.get(i).id)));
    }

    @Override
    public int getItemCount() {
        return sellColumnLinks.size();
    }
}
